home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
ORBMECHD
/
ORBMECH2.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-26
|
10KB
|
485 lines
#include "SANE.h"
#include "orbmech.h"
Str255 gFileName;
static int theVRefNum;
int linesInFolder;
extern WindowPtr myWindow;
extern ControlHandle vScroll;
extern TEHandle TEH;
extern int dirty;
extern WindowRecord wRecord;
extern Boolean gDone, gWriting;
extern DialogPtr LambertDia, HohmannDia, HillsDia, KeplerDia, whichDialog, gHelpDia;
extern DialogPtr gLamHelpDia, gHillsHelpDia, gKepHelpDia, gHohHelpDia;
extern extended gParameter;
extern Cursor gEditCursor;
/**********************/
DlgInits()
{
LambertDia = GetNewDialog( LAMBERT_ID, NIL_POINTER, 0L );
if ( LambertDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
HohmannDia = GetNewDialog( HOHMANN_ID, NIL_POINTER, 0L );
if ( HohmannDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
HillsDia = GetNewDialog( HILLS_ID, NIL_POINTER, 0L );
if ( HillsDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
KeplerDia = GetNewDialog( KEPLER_ID, NIL_POINTER, 0L );
if ( KeplerDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
gHelpDia = GetNewDialog( HELP_DIA_ID, NIL_POINTER, 0L );
if ( gHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
gLamHelpDia = GetNewDialog( LAM_H_DIA_ID, NIL_POINTER, 0L );
if ( gLamHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
gHillsHelpDia = GetNewDialog( HILLS_H_DIA_ID, NIL_POINTER, 0L );
if ( gHillsHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
gKepHelpDia = GetNewDialog( KEP_H_DIA_ID, NIL_POINTER, 0L );
if ( gKepHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
gHohHelpDia = GetNewDialog( HOH_H_DIA_ID, NIL_POINTER, 0L );
if ( gHohHelpDia == NIL_POINTER ) StopAlert( MSNG_RSRC_ALERT, NIL_POINTER );
}
/*************************/
SetUpFiles()
{
pStrCopy("\p", gFileName);
theVRefNum = 0;
}
/***************************/
WindowInit()
{
Rect destRect, viewRect;
Rect vScrollRect;
FontInfo myInfo;
int height;
myWindow = GetNewWindow( WINDOW_ID, &wRecord, -1L );
if ( myWindow == NIL_POINTER ) Alert( MSNG_RSRC_ALERT, 0L );
SetPort( myWindow );
TextFont(4);
TextSize(9);
vScrollRect = (*myWindow).portRect;
vScrollRect.left = vScrollRect.right-15;
vScrollRect.right += 1;
vScrollRect.bottom -= 14;
vScrollRect.top -= 1;
vScroll = NewControl( myWindow, &vScrollRect, "\p", 1, 0, 0, 0,
scrollBarProc, 0L);
viewRect = thePort->portRect;
viewRect.right -= S_BAR_WIDTH;
viewRect.bottom -= S_BAR_WIDTH;
InsetRect(&viewRect, 4, 4);
TEH = TENew( &viewRect, &viewRect );
SetView(thePort);
}
/********************************/
MyGrowWindow( w, p )
WindowPtr w;
Point p;
{
GrafPtr savePort;
long theResult;
int oScroll;
Rect r, oView;
GetPort( &savePort );
SetPort( w );
SetRect(&r, 80, 80, screenBits.bounds.right, screenBits.bounds.bottom);
theResult = GrowWindow( w, p, &r );
if (theResult == 0)
return;
SizeWindow( w, LoWord(theResult), HiWord(theResult), 1);
InvalRect(&w->portRect);
oView = (**TEH).viewRect;
oScroll = GetCtlValue(vScroll);
SetView(w);
HidePen();
MoveControl(vScroll, w->portRect.right - S_BAR_WIDTH, w->portRect.top-1);
SizeControl(vScroll, S_BAR_WIDTH+1, w->portRect.bottom - w->portRect.top-(S_BAR_WIDTH+1-2));
ShowPen();
SetVScroll();
AdjustText();
SetPort( savePort );
}
/*****************************/
ShowSelect()
{
register int topLine, bottomLine, theLine;
SetVScroll();
AdjustText();
topLine = GetCtlValue(vScroll);
bottomLine = topLine + linesInFolder;
if ((**TEH).selStart < (**TEH).lineStarts[topLine] ||
(**TEH).selStart >= (**TEH).lineStarts[bottomLine]) {
for (theLine = 0; (**TEH).selStart >= (**TEH).lineStarts[theLine]; theLine++)
;
SetCtlValue(vScroll, theLine - linesInFolder / 2);
AdjustText();
}
}
/***************************/
UpdateWindow(theWindow)
WindowPtr theWindow;
{
GrafPtr savePort;
GetPort( &savePort );
SetPort( theWindow );
BeginUpdate( theWindow );
EraseRect(&theWindow->portRect);
DrawControls( theWindow );
DrawGrowIcon( theWindow );
TEUpdate( &theWindow->portRect, TEH );
EndUpdate( theWindow );
SetPort( savePort );
}
/*****************************/
AdjustText ()
{
int oldScroll, newScroll, delta;
oldScroll = (**TEH).viewRect.top - (**TEH).destRect.top;
newScroll = GetCtlValue(vScroll) * (**TEH).lineHeight;
delta = oldScroll - newScroll;
if (delta != 0)
TEScroll(0, delta, TEH);
}
/****************************/
SetVScroll()
{
register int n;
n = (**TEH).nLines-linesInFolder;
if ((**TEH).teLength > 0 && (*((**TEH).hText))[(**TEH).teLength-1]=='\r')
n++;
SetCtlMax(vScroll, n > 0 ? n : 0);
}
/****************************/
SetView(w)
WindowPtr w;
{
(**TEH).viewRect = w->portRect;
(**TEH).viewRect.right -= S_BAR_WIDTH;
(**TEH).viewRect.bottom -= S_BAR_WIDTH;
InsetRect(&(**TEH).viewRect, 4, 4);
linesInFolder = ((**TEH).viewRect.bottom-(**TEH).viewRect.top)/(**TEH).lineHeight;
(**TEH).viewRect.bottom = (**TEH).viewRect.top + (**TEH).lineHeight*linesInFolder;
(**TEH).destRect.right = (**TEH).viewRect.right;
TECalText(TEH);
}
/*******************/
int DoFile( item )
int item;
{
int vRef, refNum, io;
Str255 fn;
switch (item) {
case NEW_FILE:
SetWTitle( myWindow, "\pUntitled");
ShowWindow( myWindow );
SelectWindow( myWindow );
break;
case OPEN_FILE:
if (OldFile(fn, &vRef ))
if (FSOpen(fn, vRef, &refNum)==noErr) {
if (ReadFile( refNum, TEH )) {
pStrCopy(fn, gFileName);
theVRefNum = vRef;
SetWTitle(myWindow, gFileName);
dirty = 0;
}
if (FSClose(refNum)==noErr) ;
ShowWindow( myWindow );
SelectWindow( myWindow );
TESetSelect(0, 0, TEH);
ShowSelect();
}
else FileError( "\pError opening ", fn );
break;
case CLOSE_FILE:
if (dirty) {
ParamText("\pSave changes for ╥",
(gFileName[0]==0) ? "\pUntitled" : (char *)gFileName,
"\p╙?", "\p");
switch ( Alert(PARAM_3, 0L) ) {
case AA_SAVE:
if (gFileName[0]==0) {
fn[0] = 0;
if (!SaveAs(fn, &vRef)) return(0);
}
else if (!SaveFile( gFileName, theVRefNum )) return(0);
break;
case AA_CANCEL: return(0);
case AA_DISCARD: dirty = 0;
}
}
CloseMyWindow();
dirty = 0;
break;
case SAVE_FILE:
if (gFileName[0]==0) goto saveas;
SaveFile(gFileName, theVRefNum);
break;
case SAVE_AS:
saveas:
fn[0] = 0;
if (SaveAs(fn, &vRef )) {
pStrCopy(fn, gFileName);
theVRefNum = vRef;
SetWTitle(myWindow, gFileName);
}
break;
case REVERT_FILE:
ParamText("\pRevert to last saved version of ╥",
gFileName, "\p╙?", "\p");
switch (Alert(PARAM_3, 0L)) {
case AA_SAVE:
HidePen();
TESetSelect( 0, (**TEH).teLength, TEH );
ShowPen();
TEDelete(TEH);
if ((gFileName[0]!=0) &&
(FSOpen(gFileName, theVRefNum, &refNum)==noErr)) {
dirty = !ReadFile( refNum, TEH );
/* I don't check for bad read! */
if (FSClose(refNum)==noErr) ;
}
ShowWindow( myWindow );
SelectWindow( myWindow );
UpdateWindow( myWindow );
case AA_CANCEL:
case AA_DISCARD: return(0);;
}
break;
/* insert print f */
case PAGE_SETUP:
DoPageSetUp();
break;
case PRINT_FILE:
CompactMem( 100 );
PrintText( (**TEH).hText, (long)(**TEH).teLength, (GrafPtr)myWindow,
StringWidth("\pmmmm"), 0L);
InitCursor();
break;
/* end print f */
case QUIT_APPL:
gDone = 1;
if (DoFile(CLOSE_FILE)) ExitToShell();
}
return(1);
}
/*****************************/
pStrCopy( p1, p2 )
register char *p1, *p2;
/* copies a pascal string from p1 to p2 */
{
register int len;
len = *p2++ = *p1++;
while (--len>=0) *p2++=*p1++;
}
/*************************************/
static Point SFGwhere = { 90, 82 };
static Point SFPwhere = { 106, 104 };
static SFReply reply;
SaveAs( fn, vRef )
Str255 fn;
int *vRef;
{
int refNum;
if (NewFile(fn, vRef))
if (CreateFile(fn, vRef, &refNum)) {
WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength);
FSClose( refNum );
dirty = 0;
return(1);
}
else {
FileError("\pError creating file ", fn);
}
return(0);
}
/*******************************/
SaveFile( fn, vrn )
Str255 fn;
int vrn;
{
int refNum;
if (FSOpen( fn, vrn, &refNum )==noErr) {
WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength);
dirty = 0;
FSClose( refNum );
return(1);
}
else FileError("\pError opening file ", fn);
return(0);
}
/*******************************/
NewFile( fn, vRef )
Str255 fn;
int *vRef;
{
SFPutFile(SFPwhere, "\p", fn, 0L, &reply);
if (reply.good) {
pStrCopy(reply.fName, fn);
*vRef = reply.vRefNum;
gWriting = 1;
return(1);
}
else return(0);
}
/*******************************/
OldFile( fn, vRef )
Str255 fn;
int *vRef;
{
SFTypeList myTypes;
myTypes[0]='TEXT';
SFGetFile( SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply );
if (reply.good) {
pStrCopy( reply.fName, fn );
*vRef = reply.vRefNum;
gWriting = 1;
return(1);
}
else return(0);
}
/*******************************/
CreateFile( fn, vRef, theRef )
Str255 fn;
int *vRef;
int *theRef;
{
int io;
io=Create(fn, *vRef, 'orbM', 'TEXT');
if ((io==noErr) || (io==dupFNErr)) io = FSOpen( fn, *vRef, theRef );
return( (io==noErr) || (io=dupFNErr) );
}
/*******************************/
WriteFile( refNum, p, num )
int refNum;
char *p;
long num;
{
int io; /* a real application would want to check
this return code for failures */
io=FSWrite( refNum, &num, p );
}
/*******************************/
ReadFile( refNum, textH )
int refNum;
TEHandle textH;
{
char buffer[256];
long count;
int io;
TESetSelect(0, (**textH).teLength, textH);
TEDelete( textH );
do {
count = 256;
io = FSRead( refNum, &count, &buffer );
TEInsert( &buffer, count, textH );
} while (io==noErr);
return( io==eofErr );
}
/*******************************/
FileError( s, f )
Str255 s, f;
{
ParamText(s, f,"\p", "\p");
Alert( FILE_ERROR_ALERT, 0L );
}
/********************************/
CloseMyWindow()
{
HideWindow( myWindow );
TESetSelect( 0, (**TEH).teLength, TEH );
TEDelete( TEH );
SetVScroll();
SetUpFiles();
gWriting = 0;
}